package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"oss-api/app/service"
	"oss-api/library/request"
	"oss-api/library/response"
)

var Garbage = new(GarbageApi)

type GarbageApi struct{}

// Page 分页获取回收站信息
func (g *GarbageApi) Page(c *gin.Context) {
	var param request.GarbagePageVo
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	data, err := service.Resource.GetGarbageResource(param)
	if err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	c.JSON(http.StatusOK, response.Success(data))
}

// Recovery 回收资源
func (g *GarbageApi) Recovery(c *gin.Context) {
	query, flag := c.GetQuery("ids")
	if !flag {
		c.JSON(http.StatusBadRequest, response.Fail("请求参数缺失"))
		return
	}
	if _, err := service.Resource.ThoroughDelete(query); err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	c.JSON(http.StatusOK, response.Success("清理资源数据成功"))
}

// Reduction 资源还原
func (g *GarbageApi) Reduction(c *gin.Context) {
	query := c.Query("ids")
	if query == "" {
		c.JSON(http.StatusBadRequest, response.Fail("参数缺失"))
		return
	}
	_, err := service.Resource.ResourceReduction(query)
	if err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	c.JSON(http.StatusOK, response.Success("恢复资源成功"))
}
